/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package station;

/**
 *
 * @author safe
 */
public final class DataEmpfangeneNachrichten {

//    private static Map<Long, byte[]> empfangeneNachrichtenMitEmpfangszeit = new ConcurrentHashMap<Long, byte[]>();
    private static String ourStationName = "";
    public static String lastMessageStationName = "";
    private static String lastMessageClass = "";
    private static int lastMessageSlot = -1;
    private static long lastMessageSendTime = 0;
    private static long lastMessageReceiveTime = 0;
   // private static long lastCollisionTimestamp = 0;
    private static long lastSending =0;
    public static final int diffForDetectCollision = 15;
    private static boolean sendingCollision = false;

    private DataEmpfangeneNachrichten() {
    }

    public static long getLastSending() {
        return lastSending;
    }

    public static void setLastSending(long lastSending) {
        DataEmpfangeneNachrichten.lastSending = lastSending;
    }

    public static String getOurStationName() {
        return ourStationName;
    }

    public static void setOurStationName(String ourStationName) {
        DataEmpfangeneNachrichten.ourStationName = ourStationName;
    }

    public static void setSendingCollision(boolean bool) {
        sendingCollision = bool;
    }

    public static boolean getSendingCollision() {
        return sendingCollision;
    }

    public static String getLastMessageClass() {
        return lastMessageClass;
    }

    public static int getLastMessageSlot() {
        return lastMessageSlot;
    }

    synchronized static boolean collisionWithUs(String stationName) {
        return ourStationName.equals(stationName);
    }

    synchronized static boolean collisionDetect(long actualMessageReceiveTime) {
        // wir brauchen auch diesen vergleich, weil lastMessageReceiveTime eventuell 0 ist nach collisionCleanUp
//        if (getSlotNumber(lastCollisionTimestamp) == getSlotNumber(actualMessageReceiveTime)) {
//            return true;
//        } else 
        if (getSlotNumber(lastSending) == getSlotNumber(actualMessageReceiveTime) && Math.abs(lastSending-actualMessageReceiveTime)<1000){
            return true;
        } else {
            return false;
        }
//        return false;
    }

    static int getSlotNumber(long timestamp) {
        int slotnummer = (int) (timestamp % 1000) / 40;
        return slotnummer;
    }

//    synchronized static void collisionCleanUp(long actualMessageReceiveTime) {
//        lastCollisionTimestamp = actualMessageReceiveTime;
//        //lastMessageStationName = "";
//        lastMessageClass = "";
//        lastMessageSlot = -1;
//        lastMessageSendTime = 0;
//        lastMessageReceiveTime = 0;
//    }

    static synchronized long[] setLastMessageAsValid(String stationName, String stationClass, int slotNumber, long sendTimeStamp, long receiveTimestamp) {
        long[] times = new long[2];
        if (lastMessageClass.equals("A")) {
            times[0] = lastMessageSendTime;
            times[1] = lastMessageReceiveTime;
        }
        lastMessageStationName = stationName;
        lastMessageClass = stationClass;
        lastMessageSlot = slotNumber;
        lastMessageSendTime = sendTimeStamp;
        lastMessageReceiveTime = receiveTimestamp;
        return times;
    }
}
